<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>filter、every、some</title>
  </head>
  <body>
    <script>
      const users = [
        { name: "王红元", js: 89 },
        { name: "尤玉玺", js: 99 },
        { name: "刘龙斌", js: 55 },
      ];
      // every 用于递归的检测元素，要所有元素操作都要返回真结果才为真。
      let status = users.every(function (item) {
        return item.js > 60;
      });
      console.log(status ? "全部及格" : "有人没及格");
    

      let array = ["hdcms", "houdunren"];
      let status1 = array.every(function (value, index, array) {
        console.log(value);
        // return false;
      });
      console.log(status1);

      //  every 不会改变原数组。

      // some() 方法测试数组中是不是至少有 1 个元素通过了被提供的函数测试。它返回的是一个 Boolean 类型的值。
      let arraY = ["白眉鹰王", "金毛狮王", "牛魔王"];
      let status2 = arraY.some(function (value, index, arraY) {
        console.log(value);
        return true;
      });
      console.log(status2);
      // some一直在找符合条件的值，一旦找到，则不会继续迭代下去

      // filter 函数
      let lesson = [
        { title: "装备", category: "css" },
        { title: "LVOE", category: "css" },
        { title: "chicken", category: "js" },
      ];
      let result = lesson.filter((item) => item.category == "css");
      console.log(result);

      //  封装filter函数
      let filterArr = [1, 2, 54, 62, 5, 23, 45, 2, 23, 26, 11, 66, 23];
      function filter(array, callback) {
        let newArray = [];
        for (const value of array) {
          if (callback(value) === true) {
            newArray.push(value);
          }
        }
        return newArray;
      }
      console.log(
        filter(filterArr, function (value) {
          return value > 20;
        })
      );
    </script>
  </body>
</html>
